<?php
global $_time_formats;
$_time_formats = array(
        array(60, 'just now'),
        array(90, '1 minute'),                  // 60*1.5
        array(3600, 'minutes', 60),             // 60*60, 60
        array(5400, '1 hour'),                  // 60*60*1.5
        array(86400, 'hours', 3600),            // 60*60*24, 60*60
        array(129600, '1 day'),                 // 60*60*24*1.5
        array(604800, 'days', 86400),           // 60*60*24*7, 60*60*24
        array(907200, '1 week'),                // 60*60*24*7*1.5
        array(2628000, 'weeks', 604800),        // 60*60*24*(365/12), 60*60*24*7
        array(3942000, '1 month'),              // 60*60*24*(365/12)*1.5
        array(31536000, 'months', 2628000),     // 60*60*24*365, 60*60*24*(365/12)
        array(47304000, '1 year'),              // 60*60*24*365*1.5
        array(3153600000, 'years', 31536000),   // 60*60*24*365*100, 60*60*24*365
    );

function smarty_pmvc_block_dynamic($param, $content, &$smarty) {
    return $content;
}

function smarty_pmvc_formInput($params) {
    if (!array_key_exists("name", $params)) {
        \pmvc\log\Log::error("name not specified for formInput");
    }
    $name 	= $params["name"];
    $type 	= array_key_exists("type", $params) ? $params["type"] : "text";
    $id 	= array_key_exists("id", $params) ? $params["id"] : $name;
    $value 	= array_key_exists("value", $params) ? $params["value"] : pmvc_getRequestParam($name);
    $attrs 	= array_key_exists("attrs", $params) ? $params["attrs"] : "";
    
    return "<input type=\"".htmlspecialchars($type)."\" "
        ."name=\"".htmlspecialchars($name)."\" "
        ."id=\"".htmlspecialchars($id)."\" "
        ."value=\"".htmlspecialchars($value)."\" ".$attrs."/>";
}

function smarty_pmvc_formInputCheckbox($params) {
	$params["type"] = "checkbox";
	$checked = $params["checked"]===TRUE || $params["checked"]==1 || $params["checked"]=="checked";
	$params["attrs"] = $params["attrs"].($checked?" checked=\"checked\"":"");
	return smarty_pmvc_formInput($params);
}

function smarty_pmvc_formTextArea($params) {
    if (!pmvc_hasVal($params["name"])) {
        \pmvc\log\Log::error("name not specified for formTextArea");
    }
    
    $name 	= $params["name"];
	$id 	= array_key_exists("id", $params) ? $params["id"] : $name;
    $value 	= array_key_exists("value", $params) ? $params["value"] : pmvc_getRequestParam($name);
    $attrs 	= array_key_exists("attrs", $params) ? $params["attrs"] : "";
    
    return "<textarea name=\"".htmlspecialchars($name)."\" "
        ."id=\"".htmlspecialchars($id)."\" ".$attrs.">"
        .htmlspecialchars($value)."</textarea>";
}

function smarty_pmvc_formInputPassowrd($params) {
    $params["type"] = "password";
    $params["value"] = "";
    return smarty_pmvc_formInput($params);
}

function smarty_pmvc_formSelect($params) {
    if (!array_key_exists("name", $params)) {
        \pmvc\log\Log::error("name not specified for formSelect");
    }
    if (!array_key_exists("options", $params)) {
        \pmvc\log\Log::error("options not specified for formSelect");
    }

    $name 		= $params["name"];
    $options 	= $params["options"];

    if (!is_array($options)) {
    	\pmvc\log\Log::error("options specified for formSelect not an array");
    }
    
    $multiple 	= array_key_exists("multiple", $params) ? $params["multiple"] : NULL;
    $size 		= array_key_exists("size", $params) ? $params["size"] : NULL;
    $id 		= array_key_exists("id", $params) ? $params["id"] : $name;
    $value 		= array_key_exists("value", $params) ? $params["value"] : pmvc_getRequestParam($name);
    $attrs 		= array_key_exists("attrs", $params) ? $params["attrs"] : "";

    // build the select
    $ret = "<select "
        ."name=\"".htmlspecialchars($name)."\" "
        ."id=\"".htmlspecialchars($id)."\" ";
    if ($multiple!=NULL) {
    	$ret .= "multiple=\"multiple\" ";
    }
    if ($size!=NULL) {
    	$ret .="size=\"".$size."\"";
    }
    $ret .= ">";
    foreach ($options as $val => $text) {
    	$sel = ($val==$value) ? " selected=\"selected\"" : "";
    	$ret .= "<option id=\"".htmlspecialchars($id."_".$val)
    		."\" value=\"".htmlspecialchars($val)."\"".$sel.">".$text."</option>";
    }
    $ret .= "</select>";

    // return the select
    return $ret;
}

function smarty_pmvc_displayFormGlobalErrors($params, Smarty &$smarty) {
    
	$errors = array_key_exists("errors", $params)
		? $params["errors"] 
		: $smarty->get_template_vars("formErrors");
		
    if (!pmvc_hasVal($errors) || count($errors)==0) {
        return "";
    }
	
	$start = array_key_exists("start", $params) ? $params["start"] : "<div class=\"globalErrors\">";
	$before = array_key_exists("before", $params) ? $params["before"] : "<span class=\"globalError\">";
	$after = array_key_exists("after", $params) ? $params["after"] : "</span>";
	$end = array_key_exists("end", $params) ? $params["end"] : "</div>";
	
	if (!array_key_exists("globalErrors", $errors)
		|| count($errors["globalErrors"])==0) {
        return "";
	}
	$globalErrors = $errors["globalErrors"];
	
	$ret = $start;
	foreach ($globalErrors as $error) {
		$ret .= $before.$error.$after;
	}
	$ret .= $end;
	
	return $ret;
}

function smarty_pmvc_displayFormFieldErrors($params, &$smarty) {
    
	$errors = array_key_exists("errors", $params)
		? $params["errors"] 
		: $smarty->get_template_vars("formErrors");
		
    if (!pmvc_hasVal($errors) || count($errors)==0) {
        return "";
    }
    
	$field = array_key_exists("field", $params) ? $params["field"] : NULL;
	$start = array_key_exists("start", $params) ? $params["start"] : "<div class=\"fieldErrors\">";
	$before = array_key_exists("before", $params) ? $params["before"] : "<span class=\"fieldError\">";
	$after = array_key_exists("after", $params) ? $params["after"] : "</span>";
	$end = array_key_exists("end", $params) ? $params["end"] : "</div>";
	
	if (!array_key_exists("fieldErrors", $errors)
		|| !array_key_exists($field, $errors["fieldErrors"])
		|| count($errors["fieldErrors"])==0) {
        return "";
	}
	$fieldErrors = $errors["fieldErrors"];
	
	if (!array_key_exists($field, $fieldErrors)
		|| count($fieldErrors[$field])==0) {
        return "";
	}
	
	$ret = $start;
	foreach ($fieldErrors[$field] as $error) {
		$ret .= $before.$error.$after;
	}
	$ret .= $end;
	
	return $ret;
}

function smarty_pmvc_niceDate($params, &$smarty) {
    if (!pmvc_hasVal($params["from"])) {
        \pmvc\log\Log::error("from not specified for niceDat");
    }
    
	$dateFrom = strtotime($params["from"]);
	$dateTo = array_key_exists("to", $params) ? strtotime($params["to"]) : time();

	$difference = $dateTo - $dateFrom;
	
	$message    = '';
	if ($dateFrom <= 0) {
            $message = 'a long time ago';
	} else {
		global $_time_formats;
		foreach ($_time_formats as $format) {
			if ($difference < $format[0]) {
				if (count($format) == 2) {
					$message = $format[1] . ($format[0] === 60 ? '' : ' ago');
					 break;
				} else {
					$message = ceil($difference / $format[2]) . ' ' . $format[1] . ' ago';
					break;
				}

			}
		}

	}
	
	return sprintf('<abbr title="%s">%s</abbr>',
		strftime('%a %d %b %H:%M:%S %Y', (int)$dateFrom),
		$message
	);
	
}

?>